package com.kun.admin.controller;

import com.kun.admin.authority.model.MenuItem;
import com.kun.admin.authority.service.AuthorityMenuService;
import com.kun.admin.authority.service.AuthorityUserService;
import com.kun.base.model.User;
import com.kun.base.model.UserAndInfoModel;
import com.kun.base.web.model.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by likun on 2016/8/25 0025.
 */
@Controller
@Api(description = "后台管理web主页面获取数据接口")
public class AdminIndexController {

    @Autowired
    private AuthorityMenuService menuService;
    @Autowired
    private AuthorityUserService authorityUserService;

    @RequestMapping("/")
    public String defaultUrl() {
        return "redirect:/public/index";
    }

    @RequestMapping("/public/index")
    public void index(HttpServletResponse response) throws IOException {
        response.getOutputStream().write("后端服务已启动...".getBytes());
    }

    private void setMenuUrl(ArrayList<MenuItem> menuItems) {
        if (menuItems != null && menuItems.size() > 0) {
            for (MenuItem item : menuItems) {
                if(item.getUrl()==null){
                    break;
                }
                String[] urls = item.getUrl().split("\n");
                if (urls != null && urls.length > 0) {
                    item.setUrl(urls[0]);
                }
                setMenuUrl(item.getChildren());
            }
        }
    }

    /**
     * 判断用户是否登陆
     *
     * @return
     */
    @RequestMapping("/public/logged")
    @ResponseBody
    @ApiOperation(value = "判断是否登陆", response=AjaxResult.class,
            notes = "如果用户登陆result的值为false,data中包含用户名和id,否则result为false,data为空")
    public AjaxResult logged(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user!=null){
            user.setPassword(null);
        }
        return new AjaxResult(user != null, user);
    }

    /**
     * 查找菜单数据
     * @return
     */
    @RequestMapping("/admin/findMenuTree")
    @ResponseBody
    @ApiOperation(value = "获取菜单数据", response = MenuItem.class)
    public MenuItem findMenuTreeData(@ApiParam(name = "userId", value = "用户id", required = true) Long userId){
        MenuItem menuItem = menuService.findMenuItem();
        return menuItem;
    }


    /**
     * 查找菜单数据
     * @return
     */
    @RequestMapping("/admin/findUserMenu")
    @ResponseBody
    @ApiOperation(value = "获取菜单数据", notes = "更具用户id获取该用户能看到的菜单数据", response = MenuItem.class)
    public MenuItem findUserMenu(@ApiParam(name = "userId", value = "用户id", required = true) Long userId){
        if (userId==null){
            return null;
        }
        MenuItem menuItem = menuService.findUserMenuItem(userId);
        return menuItem;
    }

    /**
     * 查找用户信息
     * @param userId 用户id
     * @return
     */
    @RequestMapping("/index/user/getUserInfo")
    @ResponseBody
    @ApiOperation(value = "获取用户信息", response=UserAndInfoModel.class,
            notes = "根据用户id获取用户信息")
    public AjaxResult findUserInfo(@ApiParam(name = "userId", value = "用户id", required = true)Long userId) {
        if (userId == null) {
            return new AjaxResult(false, "参数错误");
        }
        UserAndInfoModel userAndInfoModel = authorityUserService.getUserById(userId);
        userAndInfoModel.setPassword(null);
        return new AjaxResult(userAndInfoModel != null, userAndInfoModel);
    }
}
